package com.wkhyapp.lm.weigit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.wkhyapp.lm.R;


/**
 * Created by fangzehua on 2017/7/13.
 * 淘宝客项目
 * 邮箱:442947949@qq.com
 * QQ群:567598491
 */

public class EditTextWithDel extends EditText {
    private Drawable clearImg;
    private Context context;
    private int delSrc;

    public EditTextWithDel(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithDel);
        delSrc = ta.getResourceId(R.styleable.EditTextWithDel_delsrc, R.drawable.input_text_delete_icon1);
        ta.recycle();
        clearImg = context.getResources().getDrawable(delSrc);
        clearImg.setBounds(0, 0, clearImg.getIntrinsicWidth()+10, clearImg.getIntrinsicHeight());
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithDel);
        delSrc = ta.getResourceId(R.styleable.EditTextWithDel_delsrc, R.drawable.input_text_delete_icon1);
        clearImg = context.getResources().getDrawable(delSrc);
        clearImg.setBounds(0, 0, clearImg.getIntrinsicWidth()+10, clearImg.getIntrinsicHeight());
        ta.recycle();
        init();
    }


    private void init() {
        if (hasFocus()) {
            setDrawable();
        } else {
            setCompoundDrawables(null, null, null, null);
        }
        boolean isEnable = isEnabled();
        if (isEnable) {
            addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (hasFocus()) {
                        setDrawable();
                    } else {
                        setCompoundDrawables(null, null, null, null);
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (hasFocus()) {
                        setDrawable();
                    } else {
                        setCompoundDrawables(null, null, null, null);
                    }
                }
            });

        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (hasFocus()) {
            setDrawable();
        } else {
            setCompoundDrawables(null, null, null, null);
        }
    }

    //设置删除图片
    private void setDrawable() {
        if (length() < 1) {
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null, null, clearImg, null);
        }
    }

    // 处理删除事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Drawable[] drawables = getCompoundDrawables();
        if (clearImg != null && event.getAction() == MotionEvent.ACTION_UP && drawables[2] != null) {
            int eventX = (int) event.getX();
            int eventY = (int) event.getY();
            Rect rect = new Rect(getWidth() - 50 - getPaddingRight(), 0, getWidth(), getHeight());
            if (rect.contains(eventX, eventY))
                setText("");
        }
        return super.onTouchEvent(event);
    }

}
